package cn.ac.jaas.ljx.wordsegmentation.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.ac.jaas.ljx.wordsegmentation.domain.Document;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class DocumentReadUtil {

	public static List<Document> excelRead(String filePath) {
		File file = new File(filePath);
		List<Document> documents = new ArrayList<>();
		try {
			Workbook book = Workbook.getWorkbook(file);
			Sheet sheet = book.getSheet(0);
			for(int i=1; i<sheet.getRows(); i++) {
				Cell[] cells = sheet.getRow(i);
				if (cells[0].getContents().isEmpty()) {
					break;
				} else {
					Document document = new Document();
					document.setId(cells[0].getContents());
					document.setTitle(cells[1].getContents());
					document.setDigest(cells[2].getContents());
					documents.add(document);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return documents;
	}
}
